/**   
 * @Project:	Mybatis
 * @Title: UserDao.java
 * @Package com.sise.mybatis.dao
 * @Description: TODO(用一句话描述该文件做什么)
 * @author: zhiguo.lin   
 * @date 2016年8月17日 上午6:53:19
 * @CopyRight: CopyRight@2016
 * @version V1.0   
 */

package com.sise.mybatis.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.sise.mybatis.po.User;

/**
 * @ClassName: UserDao
 * @Description: 用于操作User数据的接口
 * @author: zhiguo.lin
 * @date 2016年8月17日 上午6:53:19
 */

public class UserDaoImpl implements UserDao {

    private SqlSessionFactory sqlSessionFactory;

    /**
     * <p>
     * Title: UserDaoImpl
     * </p>
     * <p>
     * Description: 注入SqlSessionFactory
     * </p>
     */

    public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {

        // 初始化父类参数
        super();
        // 注入sqlSessionFactory
        this.sqlSessionFactory = sqlSessionFactory;

    }

    
    /*
     * (非 Javadoc) <p>Title: findUserById</p> <p>Description: </p>
     * @param userId
     * @throws Exception
     * @see com.sise.mybatis.dao.UserDao#findUserById(java.lang.Integer)
     */

    @Override
    public User findUserById(Integer userId) throws Exception {

        // 打开sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        /*
         * test: 对应User.xml的mapper标签的namespace findUserById：对应User.xml的select 标签的id userId: 对应select标签的parameterType
         */

        User user = sqlSession.selectOne("test.findUserById", userId);

        // 关闭 sqlSession
        sqlSession.close();

        return user;
    }
    

    /*
     * (非 Javadoc) <p>Title: findUserByName</p> <p>Description: </p>
     * @param username
     * @throws Exception
     * @see com.sise.mybatis.dao.UserDao#findUserByName(java.lang.String)
     */

    @Override
    public List<User> findUserByName(String username) throws Exception {

        // 打开sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 执行查询
        List<User> userList = sqlSession.selectList("test.findUserByName", "小明");

        // 关闭 sqlSession
        sqlSession.close();

        return userList;
    }
    

    /*
     * (非 Javadoc) <p>Title: insertUser</p> <p>Description: </p>
     * @param user
     * @throws Exception
     * @see com.sise.mybatis.dao.UserDao#insertUser(com.sise.mybatis.po.User)
     */

    @Override
    public Integer insertUser(User user) throws Exception {

        // 打开sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 执行添加操作
        Integer result = sqlSession.insert("test.insertUser", user);
        
        sqlSession.commit();

        // 关闭 sqlSession
        sqlSession.close();
        
        return result;
    }

    
    /*
     * (非 Javadoc) <p>Title: deleteUser</p> <p>Description: </p>
     * @param userId
     * @throws Exception
     * @see com.sise.mybatis.dao.UserDao#deleteUser(java.lang.Integer)
     */

    @Override
    public Integer deleteUser(Integer userId) throws Exception {

        // 打开sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 执行删除操作
        Integer result = sqlSession.delete("test.deleteUser", userId);
        
        sqlSession.commit();

        // 关闭 sqlSession
        sqlSession.close();
        
        return result;
    }

    
    /* (非 Javadoc) 
    * <p>Title: modifyUser</p> 
    * <p>Description: </p> 
    * @param user
    * @return
    * @throws Exception 
    * @see com.sise.mybatis.dao.UserDao#modifyUser(com.sise.mybatis.po.User) 
    */ 
    
    @Override
    public Integer modifyUser(User user) throws Exception {
        
        // 打开sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        Integer result = sqlSession.update("test.modifyUser", user);
        
        sqlSession.commit();
        
        return result;
    }

}
